//
//  YIem_Find_FoundViewController.m
//  B段_开眼
//
//  Created by YIem on 16/4/27.
//  Copyright © 2016年 Howie. All rights reserved.
//

#import "YIem_Find_FoundViewController.h"

@interface YIem_Find_FoundViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *quDataArr;
@property (nonatomic, strong) NSMutableArray *cellDataArr;


/**
 *  分区头
 */
@property (nonatomic, strong) UIView *vi;
@property (nonatomic, strong) UIImageView *img;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIImageView *button;
@end

@implementation YIem_Find_FoundViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.quDataArr = [NSMutableArray array];
    self.cellDataArr = [NSMutableArray array];
    
    self.navigationItem.title = @"优质作者";
    self.view.backgroundColor = [UIColor whiteColor];
    // 返回
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back_normal"] style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithRed:0.2077 green:0.2077 blue:0.2077 alpha:1.0];
    
    
    //
    [self dataBase];
    
    // TableView;
    [self tableViewSubView];
    
}
- (void)tableViewSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[YIem_Find_FoundTableViewCell class] forCellReuseIdentifier:@"YIem_Find_FoundTableViewCell"];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.cellDataArr[section] count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.quDataArr.count;
}
// 区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YIem_Find_FoundCellModel *model = [self.quDataArr objectAtIndex:section];
    self.vi = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    
    self.vi.tag = 10000 + section;
    [self.vi addGestureRecognizer:tap];
    
    self.vi.backgroundColor = [UIColor whiteColor];
    self.img = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, (self.vi.frame.size.height - 10), (self.vi.frame.size.height - 10))];
    [self.img sd_setImageWithURL:[NSURL URLWithString:model.icon]];
    self.img.layer.cornerRadius = (self.vi.frame.size.height - 10) / 2;
    self.img.layer.masksToBounds = YES;
//    self.img.backgroundColor = [UIColor orangeColor];
    [self.vi addSubview:self.img];
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(self.img.frame.origin.x + self.img.frame.size.width + 10, 0, self.vi.frame.size.width - self.img.frame.size.width  - 100, self.vi.frame.size.height)];
    self.label.text = model.name;
//    self.label.backgroundColor = [UIColor redColor];
    [self.vi addSubview:self.label];
    
    self.button = [[UIImageView alloc] init];;
    self.button.frame = CGRectMake(self.vi.frame.size.width - 50, 0, self.vi.frame.size.height, self.vi.frame.size.height);
    self.button.image = [UIImage imageNamed:@"btn_back_2"];

    [self.vi addSubview:self.button];
    
    
    return self.vi;
}

- (void)tapAction:(UITapGestureRecognizer *)sender
{
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
//    NSLog(@"%ld", [tap view].tag - 10000);
    YIem_Find_Found_Sub_ViewController *sub = [[YIem_Find_Found_Sub_ViewController alloc] init];
    [sub setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    NSInteger index = [tap view].tag - 10000;
    sub.model = [self.quDataArr objectAtIndex:index];
    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:sub];
    [self presentViewController:nc animated:YES completion:nil];
//    [self.navigationController pushViewController:sub animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YIem_Find_FoundTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YIem_Find_FoundTableViewCell" forIndexPath:indexPath];
    cell.model = [self.cellDataArr[indexPath.section] objectAtIndex:indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return hEIGHT;
}
- (void)dataBase
{
    NSString *url = @"http://baobab.wandoujia.com/api/v3/pgcs?&num=20&start=0";
    
    [YIem_Find_FoundViewModel getDataWithPage:url andUpdate:YES andBlock:^(NSMutableArray *arr) {
        
        self.quDataArr = [arr objectAtIndex:0];
        self.cellDataArr = [arr objectAtIndex:1];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    }];
}

- (void)leftAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
